package interview.permutation;

public class Permutation {
	public void permutate(String source) {
		boolean[] use = new boolean[source.length()];
		StringBuilder sb = new StringBuilder();
		permutateRecursive(source.toCharArray(), source.length(), use, 0, sb);
	}

	private void permutateRecursive(char[] charArray, int length, boolean[] use, int level, StringBuilder sb) {
		if(level == length) {
			System.out.println(sb.toString());
		}
		
		for(int i = 0; i < length; i++) {
			if(!use[i]) {
				sb.append(charArray[i]);
				use[i] = true;
				permutateRecursive(charArray, length, use, level + 1, sb);
				use[i] = false;
				sb.setLength(sb.length() - 1);
			}
		}
	}
}
